package com.mpush.core.push;

import com.mpush.api.chat.*;
import com.mpush.api.protocol.PushType;
import com.mpush.api.push.PushContent;
import com.mpush.api.spi.push.PushListener;
import com.mpush.common.message.gateway.GatewayPushMessage;
import com.mpush.core.chat.ChatMessageHandler;
import com.mpush.tools.Jsons;

/**
 * 消息推送结果监听器
 *
 * @author gary.zhi
 * @date Jul 19, 2018 4:39:54 PM
 */
public class MessagePushListener implements PushListener<GatewayPushMessage> {

    private ChatMessageHandler chatMessageHandler;

    public MessagePushListener(ChatMessageHandler chatMessageHandler) {
        this.chatMessageHandler = chatMessageHandler;
    }

    public void process(GatewayPushMessage message, ChatMessageSendResult result) {
        PushMsgContent pushMsg = Jsons.fromJson(message.getContent(), PushMsgContent.class);

        if (null == pushMsg) {
            return;
        }

        PushContent pushContent = Jsons.fromJson(pushMsg.getContent(), PushContent.class);

        if (null == pushContent) {
            return;
        }

        byte type = pushContent.getType();

        if (type == PushType.CHAT.cmd) {

            ChatMessage chatMessage = Jsons.fromJson(pushContent.getContent(), ChatMessage.class);

            chatMessageHandler.updateOrSaveMsg(chatMessage, result);

        } else if (type == PushType.CREATE_SESSION.cmd) {

            if (result == ChatMessageSendResult.SUCCESS) {

                ChatSession chatSession = Jsons.fromJson(pushContent.getContent(), ChatSession.class);
                chatMessageHandler.updateSessionNotified(chatSession.getNotificationId());
            }
        }
        else if(type==PushType.CHAT_MEMBER_CHANGE.cmd){
            if (result == ChatMessageSendResult.SUCCESS) {
                MemberNotification memberNotification = Jsons.fromJson(pushContent.getContent(), MemberNotification.class);
                chatMessageHandler.updateMemberNotified(memberNotification.getNotificationId());
            }
        }
    }

    @Override
    public void onSuccess(GatewayPushMessage message, Object[] timePoints) {
        this.process(message, ChatMessageSendResult.SUCCESS);
    }

    @Override
    public void onAckSuccess(GatewayPushMessage message, Object[] timePoints) {
        this.process(message, ChatMessageSendResult.SUCCESS);
    }

    @Override
    public void onBroadcastComplete(GatewayPushMessage message, Object[] timePoints) {
    }

    @Override
    public void onFailure(GatewayPushMessage message, Object[] timePoints) {
        this.process(message, ChatMessageSendResult.FAILED);
    }

    @Override
    public void onOffline(GatewayPushMessage message, Object[] timePoints) {
        this.process(message, ChatMessageSendResult.FAILED);
    }

    @Override
    public void onRedirect(GatewayPushMessage message, Object[] timePoints) {
    }

    @Override
    public void onTimeout(GatewayPushMessage message, Object[] timePoints) {
        this.process(message, ChatMessageSendResult.FAILED);
        Jsons.toJson(timePoints);

    }

}
